import Weapon from "./weapon.js"
import LaserBullet from "./laser_bullet.js"
import {calc_distance} from "./utils.js"

class LaserWeapon extends Weapon {
    constructor(game, row, col, config) {
        super(game, row, col, config);

        this.target_spirit = null;

        if (!LaserWeapon.hasOwnProperty("weapon_frames")) {
            LaserWeapon.weapon_frames = [this.bg_draw(), this.bg_draw(1)];
        }

    }

    fire() {
        if (this.bullet_list.length === 0) {
            let position = this.map_cell.get_center_xy();
            this.bullet_list.push(new LaserBullet(this, position, null, this.config));
        }

        if (this.check_target_in_range()) return;
        this.target_spirit = this.find_spirit();
    };

    check_target_in_range() {
        if (!this.target_spirit) return false;
        if (this.target_spirit.blood <= 0) return false;

        let spirit_pos = this.target_spirit.position;
        let weapon_pos = this.map_cell.get_center_xy();
        let xy = calc_distance(weapon_pos, spirit_pos);

        return xy <= this.config.fire_range;


    };
}

export default LaserWeapon;